package com.logictech.lims.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import com.logictech.lims.service.ExperimentMaterialService;
import com.logictech.lims.service.ExperimentService;
import com.logictech.lims.service.LogService;
import com.logictech.lims.service.PreparationTypeService;
import com.logictech.lims.service.ReagentTypeService;
import com.logictech.lims.entity.Experiment;
import com.logictech.lims.entity.ExperimentMaterial;
import com.logictech.lims.entity.ReagentType;
import com.logictech.lims.dao.ExperimentMaterialDao;
import com.logictech.lims.dto.ExperimentMaterialDto;

@Service("experimentMaterialService")
public class ExperimentMaterialServiceImpl implements ExperimentMaterialService {
	@Resource
	private ExperimentMaterialDao experimentMaterialDao;
	
	@Resource
	private LogService logService;
	
	@Resource
	private ExperimentService experimentService;
	
	@Resource
	private ReagentTypeService reagentTypeService;
	
	@Resource
	private PreparationTypeService preparationTypeService;

	@Override
	public void doAdd(ExperimentMaterial experimentMaterial) {
		experimentMaterialDao.add(experimentMaterial);
		logService.doAdd("1",experimentMaterial);
	}

	@Override
	public void doEdit(ExperimentMaterial experimentMaterial) {
		experimentMaterialDao.update(experimentMaterial);
		logService.doAdd("2",experimentMaterial);
	}

	@Override
	public void doDelete(String id) {
		ExperimentMaterial experimentMaterial  = experimentMaterialDao.get(id);
		Experiment experiment = experimentService.queryById(experimentMaterial.getExperimentId().toString());
		experimentMaterial.setExperiment(experiment.getName());
		
		if("1".equals(experimentMaterial.getMaterialType())){
			// 试剂
			experimentMaterial.setMaterialName(reagentTypeService.queryById(experimentMaterial.getMaterialId()).getChineseName());
		}else if("2".equals(experimentMaterial.getMaterialType())){
			// 试液
			experimentMaterial.setMaterialName(preparationTypeService.queryById(experimentMaterial.getMaterialId()).getName());
		}
		
		logService.doAdd("3",experimentMaterial);
		experimentMaterialDao.delete(id);
	}

	@Override
	public ExperimentMaterial queryById(String id) {
		ExperimentMaterial experimentMaterial  = experimentMaterialDao.get(id);
		return experimentMaterial;
	}

	@Override
	public PageList<ExperimentMaterial> queryPageList(
		PageQuery<ExperimentMaterial> pageQuery) {
		PageList<ExperimentMaterial> result = new PageList<ExperimentMaterial>();
		Integer itemCount = experimentMaterialDao.count(pageQuery);
		List<ExperimentMaterial> list = experimentMaterialDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	public List<ExperimentMaterialDto> queryByExperimentId(String experimentId) {
		
		return experimentMaterialDao.queryByExperimentId(experimentId);
	}

	@Override
	public ExperimentMaterialDto queryDtoById(String id) {
		
		ExperimentMaterialDto experimentMaterial  = experimentMaterialDao.queryById(id);
		return experimentMaterial;
	}
	
}
